package com.yuke.cloud.service.wmc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.pmc.dto.*;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.wmc.dao.WmsStorageRecordMapper;
import com.yuke.cloud.service.wmc.dto.WmsPurchaseOrderDto;
import com.yuke.cloud.service.wmc.dto.WmsStorageRecordDto;
import com.yuke.cloud.service.wmc.entity.WmsStockIn;
import com.yuke.cloud.service.wmc.entity.WmsStorageRecord;
import com.yuke.cloud.service.wmc.service.WmsStorageRecordService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;


/**
 * Created by zn on 2020/12/25.
 */
@Service
@Transactional
public class WmsStorageRecordServiceImpl extends BaseService<WmsStorageRecord> implements WmsStorageRecordService {
    @Resource
    private WmsStorageRecordMapper wmsStorageRecordMapper;
    @Resource
    private PmcFeignApi pmcFeignApi;


    @Override
    public Integer addList(WmsStorageRecordDto wmsStorageRecordDto) {
        for (WmsStorageRecord wmsStorageRecord : wmsStorageRecordDto.getDtoList()) {
            wmsStorageRecord.setWarehouseId(wmsStorageRecordDto.getWarehouseId());
            wmsStorageRecord.setUpdateId(wmsStorageRecordDto.getUpdateId());
            wmsStorageRecord.setCreateTime(wmsStorageRecordDto.getCreateTime());
        }

        Integer num = batchSave(wmsStorageRecordDto.getDtoList());
        return num;
    }

    @Override
    @Async
    public CompletableFuture<Integer> addStorageRecorder(ProdStorageDto prodStorageDto,List<ProdSpecInfoExDto> prodSpecInfoExDtoList) {
        List<WmsStorageRecord> wmsStorageRecordList = new ArrayList<>();
        for (ProdSpecInfoExDto prodSpec : prodSpecInfoExDtoList){
            WmsStorageRecord wmsStorageRecord = new WmsStorageRecord();
            wmsStorageRecord.setSpecId(prodSpec.getSpecId());
            wmsStorageRecord.setWarehouseId(prodStorageDto.getWarehouseId());
            wmsStorageRecord.setUpdateId(prodStorageDto.getUpdateId());

            if (prodSpec != null && prodSpec.getUnitNumber().equals(1)){
                wmsStorageRecordList.add(wmsStorageRecord);
            }
        }

        if (wmsStorageRecordList.size() > 0){
            //ignore 跳过重复数据插入数据
            int rr = wmsStorageRecordMapper.addStorageRecorder(wmsStorageRecordList);
            return  CompletableFuture.completedFuture(rr);
        }else{
            return  CompletableFuture.completedFuture(0);
        }

    }

    @Override
    @Async
    public CompletableFuture<Integer> updateWmsStorageRecorder(WmsPurchaseOrderDto wmsPurchaseOrderDto) {
        List<WmsStockIn> list = wmsPurchaseOrderDto.getGoodsList();
        Integer i=0;

        //如果是退货入库，先判断商家是否允许退货，如果不允许在推出去
        if (list == null || list.size() < 1) {
            return CompletableFuture.completedFuture(0);
        }

        for (WmsStockIn wmsStockIn : list) {
            if (wmsStockIn.getInNumber() < 1) {
                continue;
            }

            List<ProdSpecApiDto> spec =  pmcFeignApi.getSpecNameBySpecIds(Arrays.asList(wmsStockIn.getSpecId()));
            if (spec == null || spec.get(0).getUnitNumber() < 1) {
                continue;
            }

            WmsStorageRecord wmsStorageRecord = new WmsStorageRecord();
            wmsStorageRecord.setWarehouseId(wmsPurchaseOrderDto.getWarehouseId());
            wmsStorageRecord.setSpecId(wmsStockIn.getSpecId());

            Integer num = wmsStockIn.getInNumber();
            //按商品的最小规格入库
            if (spec.get(0).getUnitNumber()>1 && spec.get(0).getUnitSpec() > 0){
                wmsStorageRecord.setSpecId(spec.get(0).getUnitSpec());
                num = num * spec.get(0).getUnitNumber();
            }

            List<WmsStorageRecord> select = wmsStorageRecordMapper.select(wmsStorageRecord);

            if (select.size() < 1) {
                wmsStorageRecord.setNumber(num);
                wmsStorageRecordMapper.insert(wmsStorageRecord);
            } else if (select.size() > 0) {
                wmsStorageRecord.setRepoId(select.get(0).getRepoId());
                wmsStorageRecord.setNumber(num + select.get(0).getNumber());
                wmsStorageRecordMapper.updateByPrimaryKeySelective(wmsStorageRecord);
            } else {
                return CompletableFuture.completedFuture(0);
            }
        }
        return  CompletableFuture.completedFuture(1);
    }

    @Override
    public Integer updateRecorder(WmsStorageRecord wmsStorageRecord,Integer num){
        List<ProdSpecApiDto> spec =  pmcFeignApi.getSpecNameBySpecIds(Arrays.asList(wmsStorageRecord.getSpecId()));
        if (spec == null || spec.get(0).getUnitNumber() < 1) {
            return 0;
        }

        //按商品的最小规格入库
        if (spec.get(0).getUnitNumber()>1 && spec.get(0).getUnitSpec() > 0){
            wmsStorageRecord.setSpecId(spec.get(0).getUnitSpec());
            num = num * spec.get(0).getUnitNumber();
        }

        List<WmsStorageRecord> select = wmsStorageRecordMapper.select(wmsStorageRecord);

        if (select.size() < 1) {
            wmsStorageRecord.setNumber(num);
            wmsStorageRecordMapper.insert(wmsStorageRecord);
        } else if (select.size() > 0) {
            wmsStorageRecord.setRepoId(select.get(0).getRepoId());
            wmsStorageRecord.setNumber(num + select.get(0).getNumber());
            wmsStorageRecordMapper.updateByPrimaryKeySelective(wmsStorageRecord);
        } else {
            return 0;
        }
        return 1;
    }

    @Override
    public Integer updateByWmsStorageRecord(WmsStorageRecord wmsStorageRecord){

        return wmsStorageRecordMapper.updateByWmsStorageRecord(wmsStorageRecord);
    }

    @Override
    public PageInfo<WmsStorageRecordDto> selectByCondition(WmsStorageRecordDto wmsStorageRecordDto,Integer page,Integer size) {
        //分类
        if (wmsStorageRecordDto.getParentId() != null || wmsStorageRecordDto.getCategoryId() != null) {
            ProdCategoryDto prodCategoryDto = new ProdCategoryDto();
            prodCategoryDto.setParentId(wmsStorageRecordDto.getParentId());
            prodCategoryDto.setCategoryId(wmsStorageRecordDto.getCategoryId());
            List<Long> leaf = pmcFeignApi.findLeaf(prodCategoryDto);
            wmsStorageRecordDto.setCategoryIdList(leaf);
            wmsStorageRecordDto.setCategoryId(null);
        }

        PageHelper.startPage(page, size);

        List<WmsStorageRecordDto> wmsStorageRecords = wmsStorageRecordMapper.findByCondition(wmsStorageRecordDto);

        if (wmsStorageRecords==null||wmsStorageRecords.size()<1){
            return null;
        }

        PageInfo pageInfo = new PageInfo(wmsStorageRecords);


        List<Long> categoryList = wmsStorageRecords.stream().map(WmsStorageRecordDto::getCategoryId).collect(Collectors.toList());
        for (Long aLong : categoryList) {
            System.out.println("aLong"+aLong);
        }
        List<ProdSpecApiDto> parent = pmcFeignApi.findParent(categoryList);

        if (parent==null||parent.size()<1){
            return pageInfo;
        }

        for (WmsStorageRecordDto wmsStorageRecord : wmsStorageRecords) {
            for (ProdSpecApiDto prodSpecApiDto : parent) {
                if (wmsStorageRecord.getCategoryId().equals(prodSpecApiDto.getCategoryId())){
                    wmsStorageRecord.setParentName(prodSpecApiDto.getParentName());
                    wmsStorageRecord.setCategoryName(prodSpecApiDto.getCategoryName());
                    System.out.println("prodSpecApiDto"+prodSpecApiDto);
                }
            }
        }

        pageInfo.setList(wmsStorageRecords);

        return pageInfo;
    }


}
